package Test;

import DB.CourseDB;
import Model.Course;

import java.util.ArrayList;

import static org.junit.Assert.fail;

public class CourseDBTest {

    //Ok
    @org.junit.Test
    public void testInsertCourse() throws Exception {
        CourseDB courseDB = new CourseDB();
        int rc = courseDB.insertCourse(new Course(courseDB.courseGetMax(), "Test Course"));

        System.out.println(rc);

        if (rc == 1) {
            System.out.println("Inserting course successful");
        }
        else {
            fail("Failed inserting course");
        }
    }

    //Ok
    @org.junit.Test
    public void testFindCourse() throws Exception {

        CourseDB courseDB = new CourseDB();
        int find = 4;

        Course course = courseDB.findCourse(find);

        if (course != null) {
            System.out.println(course.getId());
            System.out.println(course.getName());
        }
        else {
            fail("Failed finding course - course is null");
        }
    }

    //Ok
    @org.junit.Test
    public void testGetAllCourses() throws Exception {
        CourseDB CourseDB = new CourseDB();
        ArrayList<Course> courseObj = CourseDB.getAllCourses(false);

        if (courseObj != null) {
            for (Course c : courseObj) {
                System.out.println(c.getName());
            }
        }
        else {
            fail("No Courses found");
        }
    }

    //Ok
    @org.junit.Test
    public void testUpdateCourse() throws Exception {
        CourseDB courseDB = new CourseDB();
        String updateValue = "Test123";

        Course course = new Course(6, updateValue);

        courseDB.updateCourse(course);

        String courseName = courseDB.findCourse(6).getName();

        if (courseName.equals(updateValue)) {
            System.out.println("Update successful");
        }
        else {
            fail("Update failed");
        }  

    }

    //Ok
    @org.junit.Test
    public void testDeleteCourse() throws Exception {
        CourseDB courseDB = new CourseDB();
        Course course = new Course(1000, "Delete");
        courseDB.insertCourse(course);

        if (courseDB.findCourse(1000) != null) {

            courseDB.deleteCourse(1000);

            if (courseDB.findCourse(1000) == null) {
                System.out.println("Delete successful");
            }
            else {
                fail("Delete not successful");
            }
        }
        else {
            fail("Inserting for deletion failed");
        }
    }
}
